iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 7
0
自我挑戰組

golang leetcode 30天挑戰系列 第 7

golang leetcode 30天挑戰 7th day - jewels-in-stones

  • 分享至 

  • xImage
  •  

golang leetcode 30天挑戰 7th day - jewels-in-stones

題目解讀:

題目來源:

jewels-and-stones

原文:

You're given strings J representing the types of stones that are jewels, and S representing the stones you have. Each character in S is a type of stone you have. You want to know how many of the stones you have are also jewels.

The letters in J are guaranteed distinct, and all characters in J and S are letters. Letters are case sensitive, so "a" is considered a different type of stone from "A".

解讀:

給定一個 字串 J 其中每個字元代表一個種類的寶石,其中每個種類都只有一種
給定一個 字串 S 其中每個字元代表一個種類的石頭

想要找到一個演算法可以

算出 S中 有多少個石頭是寶石

初步解法:

初步觀察:

首先S中的每個字元都是石頭

J則代表所有種類的寶石

所以只找把每個字元去檢查是否有在J的寶石種類字串即可

初步設計:

Given a string J, a string S

Step 1: let size = length of S

step 2: let index = 0, count = 0

step 3: check if index == size go to step 6

step 4: check if J includes S[index] then count++

step 5: index=index+1 go to step 3

step 6: return count

my solution source code

numJewelsInStones.go

package jewels_stone

import "strings"

func numsJewelsInStone(J string, S string) int {
	count := 0
	for _, r := range S {
		temp_s := string(r)
		if strings.Contains(J, temp_s) {
			count++
		}
	}
	return count
}

遇到的困難

題目上理解的問題

因為英文不是筆者母語

所以在題意解讀上 容易被英文用詞解讀給搞模糊

pseudo code撰寫

一開始不習慣把pseudo code寫下來

因此 不太容易把自己的code做解析

golang table driven test不熟

對於table driven test還不太熟析

所以對於寫test還是耗費不少時間

測資的撰寫

package jewels_stone

import "testing"

func Test_numsJewelsInStone(t *testing.T) {
	type args struct {
		J string
		S string
	}
	tests := []struct {
		name string
		args args
		want int
	}{
		{
			name: "Example1",
			args: args{
				J: "aA",
				S: "aAAbbbb",
			},
			want: 3,
		},
		{
			name: "Example2",
			args: args{
				J: "z",
				S: "ZZ",
			},
			want: 0,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := numsJewelsInStone(tt.args.J, tt.args.S); got != tt.want {
				t.Errorf("numsJewelsInStone() = %v, want %v", got, tt.want)
			}
		})
	}
}

參考文章

golang test


上一篇
golang leetcode 30天挑戰 6th day number-of-steps-to-reduce-to-zero
下一篇
golang leetcode 30天挑戰 8th day - shuffle-string
系列文
golang leetcode 30天挑戰30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言